﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;

namespace PeisongWeb.admin
{
    public partial class complaintEdit : adminPage
    {
        public int cId
        {
            get
            {
                int tmp = 0;
                int.TryParse(Request.QueryString["cId"], out tmp);
                return tmp;
            }
            set
            {
                cId = value;
            }
        }
        private int checker = 7;   //现场检验员所属于角色ID=7
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("DM", 2))
                {
                    if (cId != 0)
                    {
                        getDetails();
                    }
                    else
                    {
                        ext_ComplaintDt.SelectedDate = DateTime.Now;
                    }
                    ddlCompanyBind();
                    ddlComplaintRootBind();
                    ddlComplaintSubBind();
                    ddlResultBind();
                    ddl_responsibleidBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }

            }
        }
        private void getDetails()
        {
            ComplaintEntity clEnt = logic.complaint.getBycId(cId);
            ext_ComplaintDt.SelectedDate = clEnt.ComplaintDt;
            ddl_buyer.SelectedValue = clEnt.BuyerId.ToString();
            ComplaintTypeEntity clTypeEnt = logic.complaintType.getById(clEnt.ComplaintType);
            if (clTypeEnt.ParentId==0)
            {
                ddl_ComplainttypeRoot.SelectedValue = clEnt.ComplaintType.ToString();
            }
            else
	        {
                ddl_ComplainttypeRoot.SelectedValue = clTypeEnt.ParentId.ToString();
                 ddl_ComplainttypeSub.SelectedValue = clEnt.ComplaintType.ToString();
	        }
           
            ddl_result.SelectedValue = clEnt.ComplaintResult;
            hid_proId.Value = clEnt.ProductId.ToString();
            ProductEntity proEnt = logic.product.getById(clEnt.ProductId);
            input_productNo.Value = proEnt.ProductNo;
            sp_productName.InnerText = proEnt.Name;
            hid_departmentId.Value=clEnt.DepartmentId.ToString();
            hid_DepartmentItemId.Value = clEnt.DepartmentItemId.ToString();
            DepartmentTypeEntity dtEnt = logic.departmentType.getById(clEnt.DepartmentId);
            lbl_department.Text = dtEnt.DepartmentType;
            if (clEnt.DepartmentId!=clEnt.DepartmentItemId)
            {
                lbl_DepartmentItemId.Text = logic.departmentType.getById(clEnt.DepartmentItemId).DepartmentType;
            }
            if (clEnt.ResponsibleId != 0)
            {
                ddl_responsibleid.SelectedValue = clEnt.ResponsibleId.ToString();
            }
            //ddl_responsibleid.Items.RemoveAt(0);
            ddl_sellerName.SelectedValue = clEnt.SellerId.ToString();
            hid_severityLevel.Value = clEnt.ComplaintLevel.ToString();
            lbl_severityLevel.Text = logic.sysCode.getSeverityLevelByxm(clEnt.ComplaintLevel.ToString()).Name;
            txt_remarks.Text = clEnt.Remarks;
        }

        //绑定采购企业
        private void ddlCompanyBind()
        {
            DataTable dt = logic.company.listWithCompanyType("2");
            ddl_buyer.DataSource = dt;
            ddl_buyer.DataBind();
            ddl_buyer.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }
        //绑定投诉类别（大类）
        private void ddlComplaintRootBind()
        {
            DataTable dt = logic.complaintType.getByParentId(0);
            ddl_ComplainttypeRoot.DataSource = dt;
            ddl_ComplainttypeRoot.DataBind();
            ddl_ComplainttypeRoot.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }

        //绑定投诉类别（小类）
        private void ddlComplaintSubBind()
        {
            if (ddl_ComplainttypeRoot.SelectedValue!="")
            {
                DataTable dt = logic.complaintType.getByParentId(Convert.ToInt32(ddl_ComplainttypeRoot.SelectedValue.ToString()));
                ddl_ComplainttypeSub.DataSource = dt;
                ddl_ComplainttypeSub.DataBind();
            }
            else
            {
                ddl_ComplainttypeSub.Items.Clear();
            }
        }
        //绑定投诉处理结果
        private void ddlResultBind()
        {
            ddl_result.DataSource= logic.sysCode.processResultList();
            ddl_result.DataBind();
            ddl_result.Items.Insert(0, new ListItem("--请选择处理结果--", ""));
        }
        //绑定责任部门
        private void departmentBind()
        {
            if (ddl_ComplainttypeRoot.SelectedValue!="")
            {
                int ctId;
                if (ddl_ComplainttypeSub.SelectedValue == "")
                {
                    ctId = Convert.ToInt32(ddl_ComplainttypeRoot.SelectedValue);
                }
                else
                {
                    ctId = Convert.ToInt32(ddl_ComplainttypeSub.SelectedValue);
                }
                ComplaintTypeEntity ctEnt = logic.complaintType.getById(ctId);
                int DepartmentId = ctEnt.DepartmentId;
                DepartmentTypeEntity dtEnt = logic.departmentType.getById(DepartmentId);
                lbl_DepartmentItemId.Text = dtEnt.DepartmentType;
                hid_DepartmentItemId.Value = DepartmentId.ToString();
                lbl_department.Text = logic.departmentType.getById(dtEnt.ParentId).DepartmentType;
                hid_departmentId.Value = (dtEnt.ParentId == 0 ? DepartmentId : dtEnt.ParentId).ToString();
                ddl_responsibleidBind();
                hid_severityLevel.Value = ctEnt.severityLevel;
                lbl_severityLevel.Text = logic.sysCode.getSeverityLevelByxm(ctEnt.severityLevel).Name;
            }
        }
        //绑定供应商
        private void sellerBind()
        {
            DataTable dt = new DataTable();
            if (hid_proId.Value!="0"&&hid_proId.Value!=""&&cId==0)
	        {
                dt = logic.company.listSellerByProductId(Convert.ToInt32(hid_proId.Value));   
	        }
            else
            {
                dt = logic.company.listSellerWithSellerRange();
            }
            ddl_sellerName.DataSource = dt;
            ddl_sellerName.DataBind();
            ddl_sellerName.Items.Insert(0, new ListItem("--请选择责任供应商--", ""));
        }

        //绑定责任人
        private void ddl_responsibleidBind()
        {
            int departmentId = 0;
            int.TryParse(hid_DepartmentItemId.Value, out departmentId);
            //绑定责任分类下的责任人，如：客服——xxx。
            ddl_responsibleid.DataSource = logic.sysAdmin.getBydepartmentId(departmentId);
            ddl_responsibleid.DataBind();
            if (cId==0)
            {
                ddl_responsibleid.Items.Insert(0, new ListItem("--请选责任人--"));
            }
            sellerBind();
        }
        protected void ddl_ComplainttypeRoot_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlComplaintSubBind();
            departmentBind();
            if (cId == 0)
            {
                getchecker();           //绑定客户所对应的现场操作员
            }
        }
        protected void ddl_ComplainttypeSub_SelectedIndexChanged(object sender, EventArgs e)
        {
            departmentBind();
            if (cId==0)
            {
                getchecker();           //绑定客户所对应的现场操作员
            }
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            ComplaintEntity clEnt = new ComplaintEntity();
            clEnt.ComplaintDt = ext_ComplaintDt.SelectedDate;
            clEnt.BuyerId = Convert.ToInt32(ddl_buyer.SelectedValue);
            int productId = 0;
            int.TryParse(hid_proId.Value, out productId);
            clEnt.ProductId = productId;
            clEnt.ComplaintType = Convert.ToInt32(ddl_ComplainttypeSub.SelectedValue);
            clEnt.DepartmentId = Convert.ToInt32(hid_departmentId.Value);
            clEnt.DepartmentItemId = Convert.ToInt32(hid_DepartmentItemId.Value);
            int rsId = 0;
            //int.TryParse(ddl_responsibleid.SelectedValue, out rsId);
            if (clEnt.DepartmentItemId == checker)
            {
                //int.TryParse(hid_resId.Value, out rsId);
                int.TryParse(ViewState["resId"].ToString(),out rsId);
            }
            else
            {
                int.TryParse(ddl_responsibleid.SelectedValue, out rsId);
            }
            clEnt.ResponsibleId = rsId;
            int sellerId = 0;
            int.TryParse(ddl_sellerName.SelectedValue, out sellerId);
            clEnt.SellerId = sellerId;
            clEnt.ComplaintLevel = Convert.ToInt32(hid_severityLevel.Value);
            clEnt.ComplaintResult =ddl_result.SelectedValue;
            clEnt.Remarks = txt_remarks.Text;
            clEnt.adminId = logic.sysAdmin.AdminID;
            if (cId!=0)
            {
                clEnt.ComplaintId = cId;
                logic.complaint.update(clEnt);
            }
            else
            {
                logic.complaint.insert(clEnt);
            }
            Library.Script.ClientMsgUrl("投诉信息保存成功!", "complaintList.aspx");
        }

        /// <summary>
        /// 得到每家客户所对应的现场检验员
        /// </summary>
        private void getchecker()
        {
            if (ddl_buyer.SelectedValue != "" && Convert.ToInt32(hid_DepartmentItemId.Value) == checker)
            {
                int companyId = Convert.ToInt32(ddl_buyer.SelectedValue);
                int resId = logic.inspectorChargeCompany.getByCompanyId(companyId).adminId;
                ViewState["resId"] = resId;
                hid_resId.Value = resId.ToString();
                ddl_responsibleid.Visible = false;
                lit_resName.Text = logic.sysAdmin.getById(resId).Username;
            }
            else
            {
                ddl_responsibleid.Visible = true;
                lit_resName.Text = "";
            }
        }
    }
}
